package com.zj.jdbc3Batis.tools;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: BeanHandler
 * @Description: 将结果集转换成bean对象的处理器
 * @author: 孤傲苍狼
 * @date: 2014-10-5 下午12:00:33
 */
    public class BeanHandler extends AbstractResultHandler {


    public BeanHandler(Class<?> clazz) {
        super(clazz);
    }

    public Object handler(ResultSet rs) {
        try {
            if (!rs.next()) {
                return null;
            }
            //得到结果集元数据
            ResultSetMetaData metadata = rs.getMetaData();
            int columnCount = metadata.getColumnCount();//得到结果集中有几列数据
            boolean isWrap=isWrapClass(clazz);
            if (!isWrap&& columnCount > 1) {
                throw new RuntimeException("查询列数与结果类型不匹配");
            }
            return isWrap?putResultToPOJOType(rs,metadata):putResultToSimpleType(rs);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }



}
